#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <pthread.h>
#include "cJSON.h"


#define N 64

void* respond(void *arg);
void inter_message(int datafd);
void send_message(int datafd, char* buff);

extern int errno;
static int count = 0;

int main()
{
	 int broadfd;
 
    //创建一个socket文件描述符
    broadfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (broadfd < 0)
    {
        perror("sock err");
        return -1;
    }
    //绑定套接字(ip+port)
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8080); //端口号
    addr.sin_addr.s_addr = INADDR_ANY;
 
    int addrlen = sizeof(addr);
 
    if (bind(broadfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }

	ssize_t len;
	char buf[N] = {0};
	struct sockaddr_in cliaddr;

	
	bzero(buf, N);
    len = recvfrom(broadfd, buf, N, 0, (struct sockaddr *)&cliaddr, &addrlen);
 
    //判断是否是本公司产品：收到的数据是否"group"
    if (strcmp(buf, "group") != 0)
    {
        printf("not my group\n");
        return -1;
    }
 
    //回复yes，告诉软件，我收到了搜索协议，并且回复地址
	sleep(2);
	int ret = 0;
    ret = sendto(broadfd, "yes", 4, 0, (struct sockaddr *)&cliaddr, addrlen);
	if(ret>0)
	{
		printf("发送yes\n");
	}
    bzero(buf, N);
	  //变身为TCP服务器，准备接收软件的升级文件
    int tcpfd = socket(AF_INET, SOCK_STREAM, 0);
    if (tcpfd < 0)
    {
        perror("sock err");
        return -1;
    }
 
    if (bind(tcpfd, (struct sockaddr *)&addr, addrlen) < 0)
    {
        perror("bind err");
        return -1;
    }
 
    //监听套接字
    if (listen(tcpfd, 5) < 0)
    {
        perror("listen err");
        return -1;
    }
 
    //接收客户端的连接
    printf("wait qt connect\n");
    int clifd;
 
    //接收对端的地址
    clifd = accept(tcpfd, NULL, NULL);
    if (clifd < 0)
    {
        perror("accept err");
        return -1;
    }
 
    //接收到QT端链接
    printf("qt cononect coming\n");
 
	char str[] = "connect yes";
    ret = write(clifd,str,sizeof(str));
	printf("str:%s\n",str);
	if(ret < 0)
	{
		printf("write");
	}
	else
	{
		perror("发送成功\n");
	}
	while(1)
	{
		bzero(buf,N);
		int len = recv(clifd,buf,N,0);
		cJSON *root = cJSON_Parse(buf);
		cJSON *item = cJSON_GetObjectItem(root,"type");
		int type = item->valueint;
		if(type == 3)
		{
			if(send(clifd,"living",7,0)<0)
			{
				perror("send err\n");
			}
		}

	}
}


  /*
    int servfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    struct sockaddr_in servaddr;
    
    memset(&servaddr,0,sizeof(servaddr));
    
    servaddr.sin_family = AF_INET;
     servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	//  servaddr.sin_addr.s_addr = inet_addr("127.0.0.6");
    servaddr.sin_port = htons(8080);
    
    bind(servfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
    
    listen(servfd,20);

    struct sockaddr_in clntaddr;
    socklen_t clntaddr_size = sizeof(clntaddr);

    int datafd = -1;
    pthread_t tid;
    int ret;
    while(1)
    {
	printf("1111\n");
	datafd = accept(servfd,(struct sockaddr*)&clntaddr,&clntaddr_size);
	printf("2222\n");
	if(datafd < 0)
	{
	    if(errno == EINTR)
	    {
		continue;
	    }
	    else
	    {
		printf("accept error\n");
		break;
	    }
	}
	else
	{
	    printf("可以\n");
	    ret = pthread_create(&tid,NULL,respond,&datafd);
	    if(ret)
	    {
		close(datafd);
		printf("1111\n");
		pthread_join(tid,NULL);
		datafd = -1;
	    }
	}
	
    }
    close(servfd);
    return 0;*/

void *respond(void* arg)
{
    count++;
    printf("可以阿\n");
    int datafd = *(int*)arg;
    char str[] = "连接成功！！！";
    write(datafd,str,sizeof(str));
    printf("连接了%d台\n",count);
    inter_message(datafd);
}

void rec_message(int datafd,char* buff)
{
	int ret;
	ret = read(datafd,buff,sizeof(buff)-1);
	if(ret<0)
	    {
		perror("read");
		return;
	    }
    	printf("client:%s\n",buff);
    	memset(&buff,0,sizeof(buff));

}
void send_message(int datafd, char* buff)
{
    int ret;
	fgets(buff,sizeof(buff),stdin);
	ret = write(datafd,buff,sizeof(buff));
	if(ret == -1)
	{
	    perror("write");
	    return;
	}
	else
	{
	    printf("发送成功\n");
	    memset(&buff,0,sizeof(buff));
	}
        
}

void inter_message(int datafd)
{
    char buff[40];
//    int mod_ser;
//    int mod_cle;
    while(1){
	send_message(datafd,buff);
    
/*	read(datafd,mod_ser,sizeof(mod));

	if(mod_ser == 1){
	    rec_message(datafd,buff);
	    mod_cle = 1;
	    write(datafd,mod_cle,sizeof(mod_ser));
*/	}
}













